/**
 * 角色管理（添加|修改）roleOption.js
 * 版本号： v1.0.0
 */
$(function(){

    //设置输入框的最大输入长度
    $("#name").textbox("textbox").attr("maxlength", 18);
    $("#remark").textbox("textbox").attr("maxlength", 202);

    //操作按钮点击
    $('#optionRole').bind('click', function(){
        doOptionRoleMethod();
    })

    //设置菜单权限勾选
    $.ajax({
        url: CONTEXT_PATH + "/menu/getTreeMenuByUser.do",
        type: "get",
        success: function (data) {
            data = data.replace(/meId/g,"id").replace(/name/g,"text");
            data = JSON.parse(data);
            $("#menuTree").tree({
                data:data,
                checkbox:true,
                onLoadSuccess:function(){
                    var menuIds = $("#menuIds").val();
                    menuIds = menuIds.split(",");
                    if(menuIds.length > 0 && menuIds[0]){
                        for(menuId in menuIds){
                            var node = $('#menuTree').tree('find', menuIds[menuId]);
                            if($('#menuTree').tree('isLeaf',node.target)){
                                $('#menuTree').tree('check', node.target);
                            }
                        }
                    }
                }
            });
        }
    });
});

/**
 * 获得所有选取的Id值
 */
function getCheckedIds(){
    var nodes = $("#menuTree").tree('getChecked', ['checked','indeterminate']);
    //设置所勾选的菜单权限ID，并给页面的menuIds赋值
    var menuIds = "";
    var ids =[];
    for(node in nodes){
        ids.push(nodes[node].id)
    }
    menuIds = ids.join(",");
    $("#menuIds").val(menuIds);
}

/**
 * 执行保存操作方法
 */
function doOptionRoleMethod(){
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/role/save.do",
        onSubmit:function() {
            getCheckedIds();
            var flag = $(this).form("validate");
            if(flag){
                preventSubmit(null, "optionRole");
            }
            return flag;
        },
        success:function(data) {
            var obj = JSON.parse(data);
            var url = CONTEXT_PATH + "/role/index.do";
            saveJump(obj,url);
        }
    });
}

